<?php
class LedaResolversController extends AppController {

	var $name = 'LedaResolvers';
	var $helpers = array('Html', 'Form', 'Javascript', 'Accesscount', 'Ledarlink');
	var $uses = array('LedaResolver', 'LedaProfileAuthor', 'LedaProfileCreator', 'LedaProfileTranscript', 'LedaRlink', 'Accesscount', 'StrUtil');
	var $paginate = array('limit' => 250);

	var	$_pattern = array('\s'=>'%');
	var	$_rlinkPattern = array(' |　|,|;'=>' ', '  '=>' ', ' '=>'%');

	function beforeFilter(){
		if( !empty( $this->passedArgs['au_name']) ){ // au_name をサニタイズする
			$this->passedArgs['au_name'] = strip_tags($this->passedArgs['au_name']);
		}

		parent::beforeFilter();
	}

	function index() {
		if ( empty($this->passedArgs['au_name']) ) { // au_name が空ならエラー
			$this->redirect(array('controller'=>'pages','action'=>'error'));
		}
		if ( mb_strlen($this->passedArgs['au_name']) < 4 ) {// au_name 3文字以下はエラー
			$this->redirect(array('controller'=>'pages','action'=>'error'));
		}

		// 文字列置換
		$strWhereLike = $this->StrUtil->_strRep($this->passedArgs['au_name'], $this->_pattern);
		$strRlinkWhereLike = $this->StrUtil->_strRep($this->passedArgs['au_name'], $this->_rlinkPattern);

		// 一覧表示準備用
		// contributor.authorを検索
		$this->LedaProfileAuthor->recursive = -1;
		$query['conditions'] = array('"LedaProfileAuthor"."author" ILIKE ?' => array("%{$strWhereLike}%"));
		$query['fields'] = array('id');
		$arrAuthorWhereIn = $this->LedaProfileAuthor->find('all',$query);

		// creator.alternativeを検索
		$this->LedaProfileCreator->recursive = -1;
		$query['conditions'] = array('"LedaProfileCreator"."creator" ILIKE ?' => array("%{$strWhereLike}%"));
		$query['fields'] = array('id');
		$arrCreatorWhereIn = $this->LedaProfileCreator->find('all',$query);

		// creater.transcriptを検索
		$this->LedaProfileTranscript->recursive = -1;
		$query['conditions'] = array('"LedaProfileTranscript"."creator" ILIKE ?' => array("%{$strWhereLike}%"));
		$query['fields'] = array('id');
		$arrTranscriptWhereIn = $this->LedaProfileTranscript->find('all',$query);

		// 配列再構成
		$arrAuthorWhereIn = $this->_arrReasm($arrAuthorWhereIn);
		$arrCreatorWhereIn = $this->_arrReasm($arrCreatorWhereIn);
		$arrTranscriptWhereIn = $this->_arrReasm($arrTranscriptWhereIn);

		// 抽出したIDをマージ
		$arrAsm = array_merge($arrAuthorWhereIn, $arrCreatorWhereIn);
		asort($arrAsm);
		$arrAsm = array_merge($arrAsm, $arrTranscriptWhereIn);
		$arrAsm = array_unique($arrAsm);

		// 一覧表示用検索
		$this->LedaResolver->recursive = 1;
		$scope = array('LedaResolver.id' => $arrAsm);
		// @FIXME 通常列名を「"」で括る必要は無いが、「?」以降の列名をCakeが括らないため手動で括る
		$query['conditions'] = array('"LedaRlink"."name" ILIKE ? OR "LedaRlink"."en_name" ILIKE ?' => array("%{$strRlinkWhereLike}%","%{$strRlinkWhereLike}%"));
		$query['fields'] = array('au_id','name','en_name');
		$query['order'] = array('au_id');

		$this->set('ledaResolvers', $this->paginate(null, $scope));
		$this->set('ledaRlinks', $this->LedaRlink->find('all',$query));
		$this->set('accesslogs', $this->Accesscount->_get_accesslog()); // アクセスログ
	}

	/**
	 * 検索結果をマージしやすくするため１次元配列へ
	 *
	 * @param (array) $arrArgs
	 * @return (array) $arrId
	 */
	function _arrReasm($arrArgs) {
		$arrId = array();
		if ( !is_array($arrArgs) ) return $arrId;
		foreach($arrArgs AS $item) {
			if ( isset($item['LedaProfileAuthor']['id']) ) {
				$arrId[] = $item['LedaProfileAuthor']['id'];
			} else if ( isset($item['LedaProfileCreator']['id']) ) {
				$arrId[] = $item['LedaProfileCreator']['id'];
			} else if ( isset($item['LedaProfileTranscript']['id']) ) {
				$arrId[] = $item['LedaProfileTranscript']['id'];
			}
		}
		return $arrId;
	}

}
?>